package com.est.dase;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.TabActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.location.Location;
import android.net.Uri;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TabHost.TabContentFactory;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;

import com.est.dase.database.CreateDataBaseDevice;
import com.est.dase.widget.Status;
/**
 *
 * @author Fernando Geraldes, Jaime Santos, Jorge Silva
 *
 */
public class TabBar extends TabActivity {

	private TabHost mTabHost;
	WAY way;
	public static Activity activity;

	private void setupTabHost() {
		mTabHost = (TabHost) findViewById(android.R.id.tabhost);
		mTabHost.setup();
	}

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tab_layout);

		Status.getInstance(this).setOutro(getString(R.string.status_other));
		
		String provider = android.provider.Settings.Secure.getString(this.getContentResolver(), android.provider.Settings.Secure.LOCATION_PROVIDERS_ALLOWED);

		System.out.println("provider:" + provider);

//		if(!provider.contains("gps")){
//			final Intent poke = new Intent();
//			poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider"); 
//			poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
//			poke.setData(Uri.parse("3")); 
//			this.sendBroadcast(poke);
//		}
//
//		if(!provider.contains("network")){
//			final Intent poke = new Intent();
//			poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider"); 
//			poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
//			poke.setData(Uri.parse("0")); 
//			this.sendBroadcast(poke);
//		}

		MyLocation myLocation = new MyLocation();
		Location location =  myLocation.getLocation(this);

		if(location == null){
			WifiManager wifi;
			wifi = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
			wifi.setWifiEnabled(true);
			
			final Intent poke = new Intent();
			poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider"); 
			poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
			poke.setData(Uri.parse("3")); 
			this.sendBroadcast(poke);
			
			AlertDialog.Builder builder = new AlertDialog.Builder(TabBar.this);
			Resources res = getResources();
			builder.setMessage(res.getString(R.string.tabBar_4))
			.setCancelable(false)
			.setPositiveButton("OK", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int id) {
					TabBar.this.startActivityForResult(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS), 0);
				}
			});
			AlertDialog alert = builder.create();
			alert.show();
		}

		activity=this;

		CreateDataBaseDevice createDataBaseDevice = new CreateDataBaseDevice(this);
		//createDataBaseDevice.drop();
		createDataBaseDevice.close();

		way = new WAY(this);

		setupTabHost();
		mTabHost.getTabWidget().setDividerDrawable(R.drawable.tab_divider);

		Resources res = getResources();

		setupTab(new TextView(this), res.getString(R.string.tabBar_1), R.drawable.info);
		setupTab1(new TextView(this), res.getString(R.string.tabBar_2), R.drawable.chat);
		setupTab2(new TextView(this), res.getString(R.string.tabBar_3), R.drawable.tools);


	}

	private void setupTab(final View view, final String tag,final int image) {
		View tabview = createTabView(mTabHost.getContext(), tag,  image);

		Intent intent = new Intent().setClass(this, About.class);

		TabSpec setContent = mTabHost.newTabSpec(tag).setIndicator(tabview).setContent(new TabContentFactory() {
			public View createTabContent(String tag) {return view;}
		}).setContent(intent);

		mTabHost.addTab(setContent);

	}

	private void setupTab1(final View view, final String tag,final int image) {
		View tabview = createTabView(mTabHost.getContext(), tag,  image);

		Intent intent_1 = new Intent().setClass(this, Command.class);

		TabSpec setContent = mTabHost.newTabSpec(tag).setIndicator(tabview).setContent(new TabContentFactory() {
			public View createTabContent(String tag) {return view;}
		}).setContent(intent_1);

		mTabHost.addTab(setContent);

	}

	private void setupTab2(final View view, final String tag,final int image) {
		View tabview = createTabView(mTabHost.getContext(), tag,  image);

		Intent intent_2 = new Intent().setClass(this, Settings.class);

		TabSpec setContent = mTabHost.newTabSpec(tag).setIndicator(tabview).setContent(new TabContentFactory() {
			public View createTabContent(String tag) {return view;}
		}).setContent(intent_2);

		mTabHost.addTab(setContent);

	}

	private static View createTabView(final Context context, final String text,final int image) {
		View view = LayoutInflater.from(context).inflate(R.layout.tabs_bg, null);
		TextView tv = (TextView) view.findViewById(R.id.tabsText);
		tv.setText(text);
		ImageView iv =(ImageView)view.findViewById(R.id.imageView1);
		iv.setImageResource(image);
		return view;
	}

	
}


